//
//  RegisterViewController.m
//  myMedicine1.0
//
//  Created by pollysoft on 16/7/18.
//  Copyright © 2016年 microi. All rights reserved.
//

#import "RegisterViewController.h"
#import <AVOSCloud/AVOSCloud.h>
@interface RegisterViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *registerNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *registerPhoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *registerPasswordTextField;
@property (weak, nonatomic) IBOutlet UITextField *registerPasswordConfirmTextField;
@property (weak, nonatomic) IBOutlet UITextField *VerificationCodeTextField;
@property(nonatomic,strong) UIAlertController *alertController;
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField;
- (IBAction)registerUser:(id)sender;

- (IBAction)SendVerificationCodeAction:(id)sender;


@end

@implementation RegisterViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _registerNameTextField.delegate = self;
    _registerPasswordTextField.delegate = self;
    _registerPasswordConfirmTextField.delegate = self;
    
}


//弹出键盘时，假如输入框被挡住，则输入框上移
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    CGFloat offSet = self.view.frame.size.height - (textField.frame.origin.y + textField.frame.size.height + 216 +50);
    if (offSet <= 0) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = self.view.frame;
            frame.origin.y = offSet;
            self.view.frame = frame;
            
            
        }];
    }
    
    return YES;
}

//实现回收键盘时，输入框恢复原来的位置
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [UIView animateWithDuration:YES animations:^{
        CGRect frame = self.view.frame;
        frame.origin.y = 0.0;
        self.view.frame = frame;
        
    }];
    
    return YES;
}
//隐藏自身软键盘
- (IBAction)TextField_DidEndOnExit:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)nameTextField_DidEndOnExit:(id)sender {// 将焦点移至下一个文本框.
    [_registerPhoneTextField becomeFirstResponder];
}

- (IBAction)phoneNumberTextField_DidEndOnExit:(id)sender {    // 将焦点移至下一个文本框.
    [_VerificationCodeTextField becomeFirstResponder];
}

- (IBAction)VerificationcodeTextField_DidEndOnExit:(id)sender { // 将焦点移至下一个文本框.
    [_registerPasswordTextField becomeFirstResponder];
    
}

- (IBAction)passwordTextField_DidEndOnExit:(id)sender {    // 将焦点移至下一个文本框.
    [_registerPasswordConfirmTextField becomeFirstResponder];
}
//点击背景隐藏软件盘
- (IBAction)View_TouchDown:(id)sender {
    [[UIApplication sharedApplication]sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

-(UIAlertController *)alertController{
    if (!_alertController) {
        _alertController = [[UIAlertController alloc]init];
    }
    return _alertController;
}
- (void)showAlert:(NSString *)prompt {
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
    
    _alertController =[UIAlertController alertControllerWithTitle:prompt message:nil preferredStyle:UIAlertControllerStyleAlert];
    [_alertController addAction:cancelAction];
    [_alertController addAction:okAction];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)registerUser:(id)sender {
    
    if ([_registerNameTextField.text isEqualToString:@""]) {
        [self showAlert:@"账号名不能为空"];
        [self presentViewController:_alertController animated:YES completion:nil];
        NSLog(@"看这%@",_registerNameTextField.text);
        return;
    }
    if ([_registerPhoneTextField.text isEqualToString:@""]) {
        [self showAlert:@"手机号码不能为空"];
        [self presentViewController:_alertController animated:YES completion:nil];
        return;
        
    }
    if ([_VerificationCodeTextField.text isEqualToString:@""]) {
        
        [self showAlert:@"验证码不能为空"];
        [self presentViewController:_alertController animated:YES completion:nil];
        return;
        
    }
    if ([_registerPasswordTextField.text isEqualToString:@""]) {
        
        [self showAlert:@"密码不能为空"];
        [self presentViewController:_alertController animated:YES completion:nil];
        return;
        
    }
    if ([_registerPasswordConfirmTextField.text isEqualToString:@""]) {
        
        [self showAlert:@"再次输入密码不能为空"];
        [self presentViewController:_alertController animated:YES completion:nil];
        return;
        
    }
    if (!_registerPasswordConfirmTextField.text || ![_registerPasswordConfirmTextField.text isEqualToString:_registerPasswordTextField.text]) {
        
        [self showAlert:@"请输入相同密码"];
        [self presentViewController:_alertController animated:YES completion:nil];
        return;

    }
    [AVUser signUpOrLoginWithMobilePhoneNumberInBackground:_registerPhoneTextField.text smsCode:_VerificationCodeTextField.text block:^(AVUser *user, NSError *error) {
        NSLog(@"验证失败%@",[error localizedDescription]);
        
    }];
    AVUser *user = [AVUser user];
    user.username = _registerNameTextField.text;// 设置用户名
    user.password =  _registerPasswordTextField.text;// 设置密码
//    user.email = @"834232045@qq.com";
//    user.mobilePhoneNumber = _registerPhoneTextField.text;// 设置手机号码
//    NSError *error = nil;
//    [user signUp:&error];
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            // 注册成功
            NSLog(@"注册成功,%@,%@",_registerNameTextField.text,_registerPasswordTextField.text);
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            // 失败的原因可能有多种，常见的是用户名已经存在。
            NSLog(@"%@",[error localizedDescription]);
        }
    }];
}

- (IBAction)SendVerificationCodeAction:(id)sender {
    
//    AVUser *user = [AVUser user];
//    user.username = _registerNameTextField.text;// 设置用户名
//    user.password =  _registerPasswordTextField.text;// 设置密码
//    user.mobilePhoneNumber = _registerPhoneTextField.text;// 设置手机号码
//    NSError *error = nil;
//    [user signUp:&error];
//    

//    [AVUser requestMobilePhoneVerify:_registerPhoneTextField.text withBlock:^(BOOL succeeded, NSError *error) {
//        if (succeeded) {
//            NSLog(@"验证码发送成功");
//        }
//        else{
//            [self showAlert:[error localizedDescription]];
//            NSLog(@"为什么出错呢%@",[error localizedDescription]);
//            NSLog(@"验证码发送失败%@",error);
//            NSLog(@"手机号码%@",_registerPhoneTextField.text);
//        }
//    }];
    
    
    [AVOSCloud requestSmsCodeWithPhoneNumber:_registerPhoneTextField.text callback:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    NSLog(@"验证码发送成功");
                }
                else{
                    [self showAlert:[error localizedDescription]];
                    NSLog(@"为什么出错呢%@",[error localizedDescription]);
                    NSLog(@"验证码发送失败%@",error);
                    NSLog(@"手机号码%@",_registerPhoneTextField.text);
                }
    }];
    
    
}

@end
